package com.levi.juc.pool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 自动创建线程池
 * 三大方法
 * Executors.newSingleThreadExecutor()  创建一个线程的线程池  单例模式
 * Executors.newFixedThreadPool(5)  创建固定数量线程的线程池
 * Executors.newCachedThreadPool() 创建一个可伸缩的线程池
 */
public class ExecutorsTest {
    public static void main(String[] args) {
//        ExecutorService poolService = Executors.newSingleThreadExecutor();
//        ExecutorService poolService = Executors.newFixedThreadPool(5);
        ExecutorService poolService = Executors.newCachedThreadPool();
        try {
        for (int i = 0; i < 50; i++) {
            poolService.execute(()->{
                System.out.println("线程id:"+Thread.currentThread().getId());
            });
            } }catch (Exception e) {
                e.printStackTrace();
            }finally {
            //关闭线程池
                poolService.shutdown();
            }

    }
}
